<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="显示标题" prop="quickLabel">
        <el-input
            v-model="queryParams.quickLabel"
            placeholder="请输入显示标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:sysUserQuick:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:sysUserQuick:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:sysUserQuick:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:sysUserQuick:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysUserQuickList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="显示标题" align="center" prop="quickLabel"/>
      <el-table-column label="显示图标" align="center" prop="quickIcon"/>
      <el-table-column label="跳转页面" align="center" prop="quickPath"/>
      <el-table-column label="状态" align="center" prop="quickStatus">
        <template #default="scope">
          <tc-status :status="scope.row.quickStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="quickSort"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:sysUserQuick:edit']"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:sysUserQuick:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改快捷菜单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="sysUserQuickRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择菜单" prop="quickPath">
          <el-tree-select
              v-model="form.menuId"
              :data="menuOptions"
              :props="{ value: 'id', label: 'label', children: 'children' }"
              value-key="menuId"
              placeholder="选择菜单"
              check-strictly
              @change="handleMenuChange"
              style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="显示标题" prop="quickLabel">
          <el-input v-model="form.quickLabel" placeholder="选择菜单自动加载"/>
        </el-form-item>
        <el-form-item label="显示图标" prop="quickIcon">
<!--          <el-input v-model="form.quickIcon" placeholder="选择菜单自动加载"/>-->
          <el-popover
              placement="bottom-start"
              :width="540"
              trigger="click"
          >
            <template #reference>
              <el-input v-model="form.quickIcon" placeholder="点击选择图标" @blur="showSelectIcon" readonly>
                <template #prefix>
                  <svg-icon
                      v-if="form.quickIcon"
                      :icon-class="form.quickIcon"
                      class="el-input__icon"
                      style="height: 32px;width: 16px;"
                  />
                  <el-icon v-else style="height: 32px;width: 16px;"><search /></el-icon>
                </template>
              </el-input>
            </template>
            <icon-select ref="iconSelectRef" @selected="selected" :active-icon="form.icon" />
          </el-popover>
        </el-form-item>
        <el-form-item label="跳转页面" prop="quickPath">
          <el-input v-model="form.quickPath" placeholder="选择菜单自动加载"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="quickSort">
          <el-switch
              v-model="form.quickStatus"
              :active-value="0"
              :inactive-value="1"
          />
        </el-form-item>
        <el-form-item label="排序" prop="quickSort">
          <el-input v-model="form.quickSort" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysUserQuick">
import TcStatus from '@/components/TcStatus/index';
import IconSelect from "@/components/IconSelect";
import {
  listSysUserQuick,
  getSysUserQuick,
  delSysUserQuick,
  addSysUserQuick,
  updateSysUserQuick
} from "@/api/system/sysUserQuick";
import {treeselect as menuTreeSelect, getMenuPath} from "@/api/system/menu.js";

const {proxy} = getCurrentInstance();

const sysUserQuickList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 菜单列表
const menuOptions = ref([]);
const iconSelectRef = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    quickLabel: null,
    quickIcon: null,
    quickPath: null,
    quickSort: null,
    quickStatus: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询快捷菜单列表 */
function getList() {
  loading.value = true;
  listSysUserQuick(queryParams.value).then(response => {
    sysUserQuickList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    quickLabel: null,
    quickIcon: null,
    quickPath: null,
    quickSort: null,
    quickStatus: 0,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null
  };
  proxy.resetForm("sysUserQuickRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加快捷菜单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSysUserQuick(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改快捷菜单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["sysUserQuickRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSysUserQuick(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSysUserQuick(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除快捷菜单编号为"' + _ids + '"的数据项？').then(function () {
    return delSysUserQuick(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/sysUserQuick/export', {
    ...queryParams.value
  }, `sysUserQuick_${new Date().getTime()}.xlsx`)
}

/** 查询菜单树结构 */
function getMenuTreeSelect() {
  menuTreeSelect().then(response => {
    menuOptions.value = response.data;
  });
}

/** 选择菜单变化触发 */
function handleMenuChange(target) {
  if (target) {
    getMenuPath(target).then(res => {
      if (res.code === 200) {
        if (res.data.menuType === 'C') {
          form.value.quickLabel = res.data.menuName;
          form.value.quickIcon = res.data.icon;
          form.value.quickPath = res.data.path;
        } else {
          form.value.menuId = '';
          proxy.$modal.msgError("请选择菜单");
        }
      }
    })
  }
}

/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset();
}

/** 选择图标 */
function selected(name) {
  form.value.quickIcon = name;
}

getMenuTreeSelect();
getList();
</script>
